#include "args.h"

#include <stdlib.h>

const char *argp_program_version = "kratos 1.0";
const char *argp_program_bug_address = "<adam.pioterek@student.put.poznan.pl>";

static char doc[] =
"Small utility to monitor Wi-Fi signal strength and modify Wi-Fi card power \
thereupon";
static char args_doc[] = "INTERFACE";

static struct argp_option options[] = {
    {"daemon",  'd',    0,      0,  "Run in background", 0 },
    {"interval", 'i',   "NUMBER", 0, "Time between Wi-Fi strength checks (in seconds)", 0 },
    {"logfile", 'l',    "FILE", 0,
  "Path to log (with -d defaults to /var/log/kratos.log, otherwise to stdout)",
  0 },
    {"maxpwr", 'm',     "NUMBER", 0, "Maximal tx power (default 15)", 0},
    {"verbose", 'v',    0,      0,  "Produce verbose output", 0 },
    { 0 }
};

static error_t parse(int key, char *arg, struct argp_state *state) {
    arguments *args = (arguments*)state->input;
    switch (key) {
        case 'i':
            args->interval = strtol(arg, NULL, 10);
            break;
        case 'd':
            args->daemon = 1;
            if (strcmp(args->logfile, "-") == 0) {
                args->logfile = "/var/log/kratos.log";
            }
            break;
        case 'v':
            args->verbose = 1;
            break;
        case 'l':
            args->logfile = arg;
            break;
        case 'm':
            args->maxpwr = strtol(arg, NULL, 10);

        case ARGP_KEY_ARG:
            if (state->arg_num >= 1) {
                argp_usage(state);
            }
            args->ifname = arg;
            break;

        case ARGP_KEY_END:
            if (state->arg_num < 1) {
                argp_usage(state);
            }
            break;

        default:
            return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

struct argp argp = { options, parse, args_doc, doc, 0, 0, 0 };
